<template>
    <div class="login-container">
        <van-nav-bar class="page-nav-bar">
        <van-icon
          slot="left"
          name="arrow-left"
          size="30"
          @click="$router.back()"
        />
    </van-nav-bar>
      <div class="title">
        <h4 class="title1">登录</h4>
        <p class="title2">会员优享更多！</p>
      </div>
      <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        required
        v-model="user.username"
        name="username"
        :rules="userFormRules.username"
        type="number"
        maxlength="11"
        label="手机号"
      >
      </van-field>
      <van-field
        required
        v-model="user.password"
        name="password"
        :rules="userFormRules.password"
        type="password"
        label="密码"
      >
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
        <van-button class="member-btn" block type="info" native-type="button" @click="member">
          注册
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'

export default {
  name: 'LoginIndex',
  components: {},
  data () {
    return {
      user: {
        username: '', // 手机号
        password: '' // 密码
      },
      userFormRules: {
        username: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空'
          },
          {
            pattern: /^[A-Za-z0-9]{6,10}$/,
            message: '密码格式错误，必须包含字母或数字，长度在 6-10 之间'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    member () {
      this.$router.push('/member')
    },
    async onSubmit () {
      // 1. 展示登陆中 loading
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭
      })
      // 2. 请求登录
      const data = await login(this.user)
      if (data.status === 200) {
        this.$store.commit('user/setToken', data.data.token)
        this.$store.commit('user/setUserInfo', data.data.dataValues)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } else if (data.status === 401) {
        this.$toast.fail('手机号或密码错误')
      } else {
        this.$toast.fail('登录失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  padding: 30px 15px 15px;
  .title {
    text-align: center;
    padding-bottom: 25px;
    .title1 {
      margin-top: 10px;
      margin-bottom: 14px;
      font-size: 20px;
    }
    .title2 {
      margin: 0;
      font-size: 15px;
    }
  }
  .van-icon {
    color: #000;
  }
  .van-cell {
    flex-direction: column;
  }

  .login-btn-wrap {
    padding: 40px 14px;
    .login-btn {
      background-color: black;
      border: none;
      font-size: 16px;
    }
    .member-btn {
      background-color: white;
      color: #000;
      font-size: 16px;
      border-color: #000;
      margin-top: 14px;
    }
  }
}
.van-cell--required::before {
  color: #000;
}
/deep/.van-field__control {
  border: 1px solid;
  height: 40px;
  padding-left: 10px;
}
</style>
